// AWTPanelWnd.cpp : implementation file
//

#include "stdafx.h"
//#include "AWTTest.h"
#include "AWTPanelWnd.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

IMPLEMENT_DYNAMIC(AWTPanelWnd, CWnd)

/////////////////////////////////////////////////////////////////////////////
// AWTPanelWnd

AWTPanelWnd::AWTPanelWnd(AWTComponent* comp)
{
	m_comp = comp;
}

AWTPanelWnd::~AWTPanelWnd()
{
	if (m_hWnd != NULL)
	{
		::DestroyWindow(m_hWnd);
		Detach();
	}
//		DestroyWindow();
}


BEGIN_MESSAGE_MAP(AWTPanelWnd, CWnd)
	//{{AFX_MSG_MAP(AWTPanelWnd)
	ON_WM_PAINT()
	ON_WM_ERASEBKGND()
	ON_WM_SETFOCUS()
	ON_WM_KILLFOCUS()
	ON_WM_CREATE()
	ON_WM_KEYDOWN()
	ON_WM_KEYUP()
	ON_WM_CHAR()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_GETDLGCODE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// AWTPanelWnd message handlers

void AWTPanelWnd::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	m_comp->paint(&dc);
}

BOOL AWTPanelWnd::OnEraseBkgnd(CDC* pDC) 
{
//	if (m_comp->IsKindOf(RUNTIME_CLASS(AWTContainer)))
//		((AWTContainer*)m_comp)->eraseBkground(pDC);
//	else
		m_comp->eraseBackground(pDC);
/*
	CBrush* br = m_comp->getBkBrush();
	if (br != NULL)
	{
#ifndef UNDER_CE
		::UnrealizeObject((HBRUSH)*br);
#endif
		CBrush* brOld = pDC->SelectObject(br);
		CRect rc;
		GetClientRect(&rc);
		pDC->FillRect(&rc, br);
		pDC->SelectObject(brOld);
	}
*/
	return TRUE;	
}

void AWTPanelWnd::OnSetFocus(CWnd* pOldWnd) 
{
	CWnd::OnSetFocus(pOldWnd);
	
	AWTFocusEvent e(m_comp, AWTFocusEvent::FOCUS_GAINED);
	m_comp->processFocusEvent(&e);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTFOCUSEVENT, 0, (LPARAM)&e);
}

void AWTPanelWnd::OnKillFocus(CWnd* pNewWnd) 
{
	CWnd::OnKillFocus(pNewWnd);
	
	AWTFocusEvent e(m_comp, AWTFocusEvent::FOCUS_LOST);
	m_comp->processFocusEvent(&e);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTFOCUSEVENT, 0, (LPARAM)&e);
}

int AWTPanelWnd::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	m_AWTWindow = getAWTWindow(this);
/*
	CWnd* wnd = this;
	CString str;
	TCHAR szClass[128];
	BOOL bFound = FALSE;
	do {
		wnd = wnd->GetParent();
		wnd->GetWindowText(str);
		::GetClassName(wnd->m_hWnd, szClass, 128);
		if (str == "!~AWTWindow~!")
		{
			bFound = TRUE;
			break;
		}
		else if (wnd->IsKindOf(RUNTIME_CLASS(AWTDialogPeer)))
		{
			bFound = TRUE;
			break;
		}
		else if (wnd->IsKindOf(RUNTIME_CLASS(AWTMdiChildWnd)))
		{
			bFound = TRUE;
			break;
		}
		else if (CString(szClass) == CString("#32770"))
		{
			bFound = TRUE;
			break;
		}
	} while (wnd != NULL);
	if (bFound)
		m_AWTWindow = wnd;		
*/	
	return 0;
}

void AWTPanelWnd::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	time_t t=0;
#ifndef UNDER_CE
	time(&t);
#endif
	AWTKeyEvent e(m_comp, AWTKeyEvent::KEY_PRESSED, (long)t, nFlags, nChar, nChar);
	m_comp->processKeyEvent(&e);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTKEYEVENT, 0, (LPARAM)&e);

	CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
}

void AWTPanelWnd::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	time_t t=0;
#ifndef UNDER_CE
	time(&t);
#endif
	AWTKeyEvent e(m_comp, AWTKeyEvent::KEY_RELEASED, (long)t, nFlags, 0, nChar);
	m_comp->processKeyEvent(&e);
	
	CWnd::OnKeyUp(nChar, nRepCnt, nFlags);
}

void AWTPanelWnd::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	time_t t=0;
#ifndef UNDER_CE
	time(&t);
#endif
	AWTKeyEvent e(m_comp, AWTKeyEvent::KEY_TYPED, (long)t, nFlags, nChar, nChar);
	m_comp->processKeyEvent(&e);
	if (nChar == VK_TAB)
		return;
	if (nChar == VK_RETURN)
		return;
	
	CWnd::OnChar(nChar, nRepCnt, nFlags);
}

void AWTPanelWnd::OnLButtonDown(UINT nFlags, CPoint point) 
{
	time_t t = 0;
#ifndef UNDER_CE
	time(&t);
#endif
	AWTMouseEvent e(m_comp, AWTMouseEvent::MOUSE_CLICKED, (long)t, 0, 
		point.x, point.y, 1, FALSE);
	m_comp->processMouseEvent(&e);
	CWnd::OnLButtonDown(nFlags, point);
}

void AWTPanelWnd::OnLButtonUp(UINT nFlags, CPoint point) 
{
	time_t t = 0;
#ifndef UNDER_CE
	time(&t);
#endif
	AWTMouseEvent e(m_comp, AWTMouseEvent::MOUSE_RELEASED, (long)t, 0, 
		point.x, point.y, 0, FALSE);
	m_comp->processMouseEvent(&e);
	CWnd::OnLButtonUp(nFlags, point);
}

void AWTPanelWnd::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	time_t t = 0;
#ifndef UNDER_CE
	time(&t);
#endif
	AWTMouseEvent e(m_comp, AWTMouseEvent::MOUSE_CLICKED, (long)t, 0, 
		point.x, point.y, 2, FALSE);
	m_comp->processMouseEvent(&e);
	CWnd::OnLButtonDblClk(nFlags, point);
}

UINT AWTPanelWnd::OnGetDlgCode() 
{
	// TODO: Add your message handler code here and/or call default
	
	return DLGC_WANTALLKEYS;
}
